package com.zw.mes.module.assemble.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.assemble.entity.AsseAssemblySheet;
import com.zw.mes.module.assemble.model.TestModel;
import com.zw.mes.module.assemble.service.ReceiveService;
import com.zw.mes.module.system.mapper.UserMapper;
import com.zw.mes.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 装配派工单 转序接收、历史接收
 * <p>
 * 权限：
 * @Author: China.wsg
 * @Date: 2021/04/27 10:53
 */
@Controller
@RequestMapping("/assemble/receive")
@SuppressWarnings("all")
public class ReceiveController extends BaseController {

    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/addPage")
    @RequiresPermissions({"asseOrd:receive:view"})
    public String addPage(Model model) {
        model.addAttribute("user", userMapper.findByAccount(ShiroUtils.getUser().getAccount()));
        return "assemble/receive/receiveAdd";
    }

    /**
     * 查询待接收列表
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    @ResponseBody
    @PostMapping("/receiveList")
    @RequiresPermissions({"asseOrd:receive:view"})
    public JsonResult receiveList(String barCode) {
        startPage();
        List<TestModel> list = receiveService.findRecListByBarCode(barCode);
        return jsonResult(list);
    }

    /**
     * 获取装配派工单信息
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    @PostMapping("/asseSheet")
    @ResponseBody
    @RequiresPermissions({"asseOrd:receive:view"})
    public JsonResult findAsseSheet(String barCode) {
        JsonResult jsonResult = new JsonResult();
        AsseAssemblySheet assemblySheet = receiveService.findAsseSheetByCode(barCode);
        jsonResult.setData(assemblySheet);
        jsonResult.setType(assemblySheet == null ? JsonResult.ResultType.ERROR :
                JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 转序接收
     *
     * @param id 转序单ID
     * @return
     */
    @ResponseBody
    @PostMapping("/receive")
    @RequiresPermissions({"asseOrd:receive:edit"})
    public JsonResult receive(Integer id) {
        return jsonResult(receiveService.receive(id));
    }

    @GetMapping("/selPage")
    @RequiresPermissions({"asseOrd:historical:view"})
    public String selPage() {
        return "assemble/receive/receiveSel";
    }

    /**
     * 派工单-历史接收列表
     *
     * @param applyModel
     * @return
     */
    @ResponseBody
    @PostMapping("/selList")
    @RequiresPermissions({"asseOrd:historical:view"})
    public JsonResult selList(TestModel testModel) {
        startPage();
        List<TestModel> list = receiveService.findHistoryRecListByQuery(testModel);
        return jsonResult(list);
    }

    /**
     * 撤销转序接收
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"asseOrd:historical:success"})
    public JsonResult cancel(Integer id) {
        return jsonResult(receiveService.cancelRec(id));
    }

}
